package com.quanquan.management.controller;

import DTO.PageDTO;
import com.quanquan.management.dto.ConsumptionAmountDTO;
import com.quanquan.management.request.ConsumptionAmountRequestBosy;
import com.quanquan.management.service.ConsumptionAmountService;
import com.quanquan.management.service.StoreService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import web.response.ResultBody;

/**
 * @ClassName:ConsumptionAmountController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/9 16:05
 * @Version 1.0
 */
@Controller
@RequestMapping("/consumption_amount")
public class ConsumptionAmountController extends ManagerBaseController{

    private final ConsumptionAmountService consumptionAmountService;

    @Autowired
    public ConsumptionAmountController(ConsumptionAmountService consumptionAmountService) {
        this.consumptionAmountService = consumptionAmountService;
    }

    @Autowired
    private StoreService storeService;
    @GetMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String phone,
                       @RequestParam(required = false) String toDate, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }

        ConsumptionAmountRequestBosy consumptionAmountRequestBosy = new ConsumptionAmountRequestBosy();
        consumptionAmountRequestBosy.setPage(page);
        consumptionAmountRequestBosy.setSize(pageSize);
        consumptionAmountRequestBosy.setKeyword(keyword);
        consumptionAmountRequestBosy.setPhone(phone);
        consumptionAmountRequestBosy.setToDate(toDate);
        consumptionAmountRequestBosy.setFromDate(fromDate);
        PageDTO<ConsumptionAmountDTO> pageDTO = consumptionAmountService.list(consumptionAmountRequestBosy);
        model.addAttribute("pageObj", pageDTO);
        return "consumptionAmount/list";
    }


    @RequestMapping("/export")
    public ResultBody exportData(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String fromDate,
            @RequestParam(required = false) String toDate,
            @RequestParam(required = false) Integer total,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String ticketNum
    ) {
        ConsumptionAmountRequestBosy consumptionAmountRequestBosy = new ConsumptionAmountRequestBosy();
        consumptionAmountRequestBosy.setKeyword(keyword);
        consumptionAmountRequestBosy.setSize(total);
        consumptionAmountRequestBosy.setPhone(phone);
        consumptionAmountRequestBosy.setTicketNum(ticketNum);
        consumptionAmountRequestBosy.setFromDate(fromDate);
        consumptionAmountRequestBosy.setToDate(toDate);
        try{
            consumptionAmountService.export(this.response,consumptionAmountRequestBosy);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }


}
